package com.dxx.cloud.admin.controller.admin;


import com.dxx.cloud.admin.enums.ErrorEnum;
import com.dxx.cloud.admin.mapper.MerchantMapper;
import com.dxx.cloud.admin.module.entity.MerchantEntity;
import com.dxx.cloud.admin.module.request.MerchantAddRequest;
import com.dxx.cloud.admin.module.request.MerchantCheckPasswordRequest;
import com.dxx.cloud.admin.module.request.MerchantPageRequest;
import com.dxx.cloud.admin.module.request.MerchantUpdateRequest;
import com.dxx.cloud.admin.module.response.UserResponse;
import com.dxx.cloud.admin.service.IUserService;
import com.dxx.cloud.admin.utils.BaseControllerUtils;
import com.dxx.cloud.admin.utils.EntityUtils;
import com.dxx.cloud.admin.utils.Md5Utils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author duh
 * @create 2018/12/28 13:47

 **/
@RestController
@RequestMapping("/merchant")
@Tag(name = "商户模块")
public class MerchantController {

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private IUserService userService;


    @Operation(summary = "添加商户")
    @RequestMapping(value = "/addMerchant", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R addMerchant(@RequestBody @Validated MerchantAddRequest request) throws Exception {
        request.setCreator(request.getOperator());
        return BaseControllerUtils.add(merchantMapper, MerchantEntity.class,request);
    }

    @Operation(summary = "修改商户")
    @RequestMapping(value = "/updateMerchant", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R updateMerchant(@RequestBody @Validated MerchantUpdateRequest request) throws Exception {
        request.setUpdater(request.getOperator());
        return BaseControllerUtils.updateById(merchantMapper,request.getId(),request);
    }

    @Operation(summary = "查询商户列表")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public TableDataInfo<MerchantEntity> pageList(@RequestBody @Validated MerchantPageRequest request) throws Exception {
        return EntityUtils.pageInfoModel(merchantMapper, MerchantEntity.class, request);
    }

    @Operation(summary = "校验商户密码")
    @RequestMapping(value = "/checkPassword", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public R checkPassword(@RequestBody @Validated MerchantCheckPasswordRequest request) throws Exception {
        R responseModel = new R();
        request.setPassword(Md5Utils.encode(request.getPassword()));
        UserResponse user = userService.selectMerByPassword(request);
        if(null == user){
            R.fail(ErrorEnum.MER_PASSWORD_IS_WRONG.getMessage());
        }
        return responseModel;
    }
}
